package com.bjtu.ptsite.service.impl;

import com.bjtu.ptsite.mapper.InviteCodeMapper;
import com.bjtu.ptsite.model.InviteCode;
import com.bjtu.ptsite.model.User;
import com.bjtu.ptsite.service.InviteCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author Zehao Wang, Junjie Zhang, Yutao Wei
 *
 */
@Service
public class InviteCodeServiceImpl implements InviteCodeService {
    @Autowired
    private InviteCodeMapper inviteCodeMapper;

    @Override
    public InviteCode validateInviteCode(String code) {
        InviteCode inviteCode = inviteCodeMapper.selectByCode(code);
        if (inviteCode == null || 
            inviteCode.getRemainingUses() <= 0 || 
            inviteCode.getExpiryTime().isBefore(LocalDateTime.now())) {
            return null;
        }
        return inviteCode;
    }

    @Override
    public InviteCode getInviteCode(String code){
        InviteCode inviteCode = inviteCodeMapper.selectByCode(code);
        return inviteCode;
    }

    @Override
    public boolean useInviteCode(String code) {
        return inviteCodeMapper.decrementRemainingUses(code) > 0;
    }

    @Override
    public InviteCode createInviteCode(User user, String code) {
        // 检查邀请码是否已存在
        InviteCode existingByCode = inviteCodeMapper.selectByCode(code);
        if (existingByCode != null) {
            throw new RuntimeException("邀请码已被使用");
        }

        // 创建新邀请码
        InviteCode newCode = new InviteCode();
        newCode.setCode(code);
        // 最大使用次数设为1
        newCode.setMaxUses(1);
        newCode.setRemainingUses(1);
        // 默认7天有效期
        newCode.setExpiryTime(LocalDateTime.now().plusDays(7));
        newCode.setCreatedBy(user.getUserId());

        inviteCodeMapper.insertCode(newCode);

        return newCode;
    }

    @Override
    public InviteCode addInviteCode(User user, String code) {
        // 检查邀请码是否匹配
        InviteCode curCode = inviteCodeMapper.selectByCode(code);
        if (curCode == null || curCode.getCreatedBy()!=user.getUserId()) {
            throw new RuntimeException("邀请码不存在");
        }

        inviteCodeMapper.addCodeUses(curCode.getCode());
        curCode = inviteCodeMapper.selectByCode(code);

        return curCode;
    }

    @Override
    public boolean deleteInviteCode(User user, String code) {
        // 检查邀请码是否匹配
        InviteCode curCode = inviteCodeMapper.selectByCode(code);
        if (curCode == null || curCode.getCreatedBy()!=user.getUserId()) {
            throw new RuntimeException("邀请码不存在");
        }

        if(inviteCodeMapper.deleteCode(code)){
            return true;
        }
        return false;
    }

    public enum UserLevel {
        /**level1*/
        NORMAL(1),
        /**level2*/
        VIP(2),
        /**level3*/
        MANAGER(3),
        /**level4*/
        ADMIN(4);

        private final int level;

        UserLevel(int level) {
            this.level = level;
        }

        public int getLevel() {
            return level;
        }
    }

    @Override
    public List<InviteCode> getCodes(User user){
        if(user.getLevel() < UserLevel.ADMIN.getLevel()){
            return null;
        }
        List<InviteCode> inviteCodes = inviteCodeMapper.getCodesById(user.getUserId());
        if(inviteCodes.size()==0){
            return null;
        }
        return(inviteCodes);
    }
}
